package com.yz.javaagent.core.transformer;

import javassist.*;

import java.io.IOException;

/**
 * @author shanchong
 * @date 2019-06-25
 **/
public class ClassUtil {


    public static byte[] getClassFileByte(String className,String methodName){
        CtClass ctClass = null;
        try {
            ctClass = ClassPool.getDefault().getCtClass(className.replace("/","."));
            CtMethod method = ctClass.getDeclaredMethod(methodName);
            method.insertBefore("{ System.out.println(\"who are you?\");}");
            method.insertAfter("{ System.out.println(\"hello world\");}");
            method.insertAfter("{ System.out.println(\"-----------------\");}");
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (CannotCompileException e) {
            e.printStackTrace();
        }
        try {
            return ctClass.toBytecode();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CannotCompileException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

}
